/**
* Class to manage a generic field properties
*
* Project: idbCrownInServer
* Author:  TM
* Date:    07/04/2013
*
* History
* ------------------
* 07/04/2013 - TM - Initial version.
*/
 
 
package DataModel;

import java.sql.*;
import java.util.*;
import java.text.*;

public class libTimestampField extends libField
{
   /** Variables */
   protected static String    FORMAT_TIMESTAMP = "dd/MM/yyyy hh:mm:ss";
   protected SimpleDateFormat dateFormat       = new SimpleDateFormat(FORMAT_TIMESTAMP);

   /** Constructor */
   public libTimestampField (String name)
   {
	    this(name, false);
   }

   /** Constructor */
   public libTimestampField (String name, boolean key)
   {
	    super(name, key);
   }
   
   /** public assign value into prepareStatement */
   public void assignValue(int index, PreparedStatement pst) throws Exception
   {
     Object             sourceValue = getValue();
     java.sql.Timestamp result      = new Timestamp(System.currentTimeMillis());
     
     try
     {
       if (sourceValue != null)
       {
         //sourceValue should be like this "07/04/2013 12:45:32"
         java.util.Date parsedDate = dateFormat.parse(sourceValue.toString());
         result = new java.sql.Timestamp(parsedDate.getTime());
       }
     }
     catch(Exception e)
     {
       debug("Error parsing date:"+sourceValue+" Used:"+result);
     }
     
     pst.setTimestamp(index, result);
   }
   
   //Static Support
   
   /** Get now */
   public static java.sql.Timestamp now()
   {
     return new Timestamp(System.currentTimeMillis());
   }
   
}
